﻿using AK56.Domain.Services;
using AKCY.Tools;
using AKCY.Tools.GuidEx;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;
using System.Web.Http.Filters;

namespace AK56.APP.Application.Home.Controllers
{
    public class QRCodeController : ApiController
    {
        private ISYS_UsersService usersService;
        private string baceUrl = AK56.Domain.Untils.WebToolkit.GetAppSetting("QRServer");
        public QRCodeController(ISYS_UsersService usersService)
        {
            this.usersService = usersService;
        }

        /// <summary>
        /// 二维码生成
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage QRGenerationForReg(string sessionId )
        {
            if (string.IsNullOrEmpty(sessionId))
            {
                throw new ArgumentNullException("sessionId");
            }
            string qrCode = GuidHelper.NewGuid().ToString();
            var rst = baceUrl + "QrCode?action=REG&sessionId=" + sessionId + "&qrCode=" + qrCode;
            return Request.CreateResponse(HttpStatusCode.OK, rst);
          
        }

        /// <summary>
        /// 取消注册
        /// </summary>
        /// <param name="username"></param>
        /// <param name="qrc"></param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage QRUnRegiste(string sessionId)
        {
            var user = usersService.GetEntity(t => t.U_SessionId == sessionId);
            if (user != null)
            {
                user.U_QRCode = "";
                usersService.Update(user);
                return Request.CreateResponse(HttpStatusCode.OK, user);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.NotImplemented, "用户未找到！");
            }
        }

    }
}
